package www.openteam.com.base;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import butterknife.Bind;
import www.openteam.com.R;
import www.openteam.com.bean.DetailGather;
import www.openteam.com.bean.QueryCondition;
import www.openteam.com.tool.ToastUtil;
import www.openteam.com.ui.DetailedActivity;

/**
 * Created by hyd on 2016/10/16.
 */

public class BaseFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {

    public static final int REQUEST_COUNT = 10;
    public DetailGather detailGather;   // 详细信息的枚举类
    public String[] detail;
    public SwipeRefreshLayout mSwipeRefresh;  //刷新布局
    public RecyclerView mRecyclerView;
    public LinearLayoutManager linearLayoutManager;
    public QueryCondition queryCondition;
    public long endMilli;
    public final String TAG = "BaseFragment";
    public boolean isLoading = false;
    int i = 0;
    @Bind(R.id.ll_mScroll)
    LinearLayout ll_mScorll;
    @Bind(R.id.iv_arrow)
    ImageView iv_arrow;

    public   GestureDetectorCompat gestureDetectorCompat;        //手势处理
    public boolean arrow_statue = true;
    public Animation animation;
    public float disY;
    public float a_disY;
    public float disMul;
    public int lastY;
    public Throwable throwable = null;
    public FragmentManager fm;
    public  FragmentTransaction localFragmentTransaction;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        detailGather = ((DetailedActivity) getActivity()).getDetailGather();   //得到本Fragment依附的Activity传递的detailGather
        fm = getActivity().getSupportFragmentManager();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view;

        Resources res = getResources();
        detail = res.getStringArray(detailGather.getResDetial());       //得到detailGather的所携带的string-array
        switch (detail.length) { //根据detail所包含string-arry的item数量选择需要的布局
            case 1:
                view = inflater.inflate(R.layout.fragment_detail1, container, false);
                break;
            case 2:
                view = inflater.inflate(R.layout.fragment_detail2, container, false);
                break;
            case 3:
                if(detail[1].substring(detail[1].toString().length()-3).equals("日期起")){

                    view = inflater.inflate(R.layout.fragment_detail3_2, container, false);
                }else {
                    view = inflater.inflate(R.layout.fragment_detail3, container, false);
                }
                break;
            case 5:
                view = inflater.inflate(R.layout.fragment_detail5, container, false);
                break;
            default:
                if (detail[1].toString().equals("单据状态")) {
                    view = inflater.inflate(R.layout.fragment_detail4_2, container, false);
                }else{
                    view = inflater.inflate(R.layout.fragment_detail, container, false);
                }
                break;
        }

//        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        initButton(view);
        gestureDetectorCompat = new GestureDetectorCompat(getActivity(), new MyGestureDetector());
        iv_arrow = (ImageView) view.findViewById(R.id.iv_arrow);
        ll_mScorll = (LinearLayout) view.findViewById(R.id.ll_mScroll);
        iv_arrow.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDetectorCompat.onTouchEvent(event);

                return true;
            }
        });

        return view;
    }

    public void initSRView(View view ) {


        mSwipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_widget);
        mSwipeRefresh.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimaryDark, R.color.colorAccent);
        mSwipeRefresh.setOnRefreshListener(this);


        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(linearLayoutManager);

    }


    public void initButton(View view){

    }
    @Override
    public void onRefresh() {

    }


    /**
     * 手势监听
     */
    public class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

//            int top = (int) (disY - distanceY / 2);
//            int height = (int) (top + disY);
//            ll_mScorll.layout(0, -top, 0, height);
            ToastUtil.showToast(getActivity(), "getY:" + e2.getY() + ",getRawY:" + e2.getRawY());
            return super.onScroll(e1, e2, distanceX, distanceY);
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//


            return super.onFling(e1, e2, velocityX, velocityY);
        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            ToastUtil.showToast(getActivity(), "onSingleTapup");
            return super.onSingleTapUp(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {
            super.onLongPress(e);

//            ToastUtil.showToast(getActivity(),"onLongPress" );
        }

        @Override
        public void onShowPress(MotionEvent e) {
            super.onShowPress(e);
//            ToastUtil.showToast(getActivity(),"onShowPress");
        }


        @Override
        public boolean onDown(MotionEvent e) {
//            ToastUtil.showToast(getActivity(),"onDown");

            return super.onDown(e);

        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {

//            ToastUtil.showToast(getActivity(),"onDoubleTap");
            return super.onDoubleTap(e);

        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {

            if (arrow_statue) {
                animation = AnimationUtils.loadAnimation(
                        getActivity(), R.anim.ll_scroll_animation);
                iv_arrow.startAnimation(animation);
                ll_mScorll.startAnimation(animation);
                mSwipeRefresh.startAnimation(animation);
                animation.setAnimationListener(new MyAnimationListenr());

            } else {
                Animation ll_translatAnimation = getArrowAnim(0, 0, -disY, 0, 1500);
                Animation iv_translatAnimation = getArrowAnim(0, 0, 0, disY, 1500);
                ll_mScorll.startAnimation(ll_translatAnimation);
                iv_arrow.startAnimation(iv_translatAnimation);
                mSwipeRefresh.startAnimation(iv_translatAnimation);
                iv_translatAnimation.setAnimationListener(new  MyAnimationListenr());
            }
            return super.onDoubleTapEvent(e);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
//            ToastUtil.showToast(getActivity(),"onSingleTapConfirmed");
            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onContextClick(MotionEvent e) {
//            ToastUtil.showToast(getActivity(),"onContextClik");
            return super.onContextClick(e);
        }
    }


    /**
     * 动画
     */
    public  class MyAnimationListenr implements Animation.AnimationListener {
        @Override
        public void onAnimationStart(Animation animation) {
            if (arrow_statue) {
                iv_arrow.setImageResource(R.drawable.icon_up_arrow);
            } else {
                iv_arrow.setImageResource(R.drawable.icon_down_arrow);
            }
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (arrow_statue) {
                ll_mScorll.setVisibility(View.GONE);

            } else {
                ll_mScorll.setVisibility(View.VISIBLE);
            }
            arrow_statue = !arrow_statue;
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }


    public Animation getArrowAnim(float a, float b, float c, float d, long time) {
        disMul = disY / a_disY;

        TranslateAnimation translateAnimation = new TranslateAnimation(a, b, c, d);
        translateAnimation.setDuration(time);


        return translateAnimation;

    }

    /**
     * 测量布局
     */
    public void measurementLayoutHeight() {
        ViewTreeObserver vto = ll_mScorll.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {


            @Override
            public void onGlobalLayout() {
                ll_mScorll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                disY = ll_mScorll.getHeight();
            }
        });
        ViewTreeObserver vto_arr = iv_arrow.getViewTreeObserver();
        vto_arr.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                iv_arrow.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                a_disY = iv_arrow.getHeight();
            }
        });


    }

}
